// components.cpp
// Customizer_Pro
// Protea Customizer Pro / Business Logic for managing components
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "pro_customizer.h"

// COMPONENT_DEFN Subtransactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 3
//@ CHA Component Add
//@ COD ComponentAdd
//@ TAB COMPONENT_DEFN Write
//@ GEN Use ComponentAdd to add a Component Defn record
void ComponentAdd( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( ComponentAdd );

	XCDBStatement* stmt = NULL;
	XCBuffer* temp = NULL;
	XCSQLGen* query = vars->query;

	long last_number = 0;

	//@ Determine the next available component id number
	temp = App->CreateBuffer( FMT_COMPONENT_DEFN );
	try {
		XCOrder ord;
		XCCompare comp;
		bool found = false;

		query->MakeSELECT( temp );
		query->AddWHERE( buf->GetField( FLD_APPL_NBR ), &comp );
		query->AddORDERBY( FLD_COMPONENT_ID, &ord );
		stmt = vars->dbtxn->ExecuteSQL( query );
		while ( !found && !stmt->Eof() )
		{
			long value = temp->GetField( FLD_COMPONENT_ID )->GetLong( true );
			if ( value != last_number + 1 )
			{
				// we found a blank space
				found = true;
				last_number++;
			}
			else
			{
				last_number = value;
				stmt->MoveNext();
			};
		};

		// Cleanup now
		App->CleanupObjects( 2, &temp, &stmt );

		if (!found)
			last_number++;
		buf->GetField( FLD_COMPONENT_ID )->SetLong( last_number );

	} catch( ... ) {
		App->CleanupObjects( 2, &temp, &stmt );
		throw;
	};

	//@ Add the component defn record
	query->MakeFull( SQL_STMT_INSERT, buf );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};



//@ SEC 3
//@ CHA Component Delete
//@ COD ComponentDelete
//@ TAB COMPONENT_DEFN Write
//@ GEN Use to delete Component Defn records
void ComponentDelete( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( ComponentDelete );

	XCSQLGen* query = vars->query;

	//@ Delete the system values record
	query->MakeFull( SQL_STMT_DELETE, buf );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};



// COMPONENT_DEFN Transactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 2
//@ CHA Add Component
//@ COD AddComponent
//@ NUM 36
//@ CRI COMPONENT_NAME
//@ GEN Add Component will add a component for an application
TRANSACTION_CODE( AddComponent )
{
	XCBuffer* buf = NULL;

	//@ GRS Start the transaction
	START_TXN( AddComponent );

		//@ If critical fields are blank, then
		//@ ERR E (1730) (Field Name) cannot be blank
		if ( txnvars.buffer->GetField( FLD_COMPONENT_NAME )->IsBlank() )
			txnvars.Errors->Add( ERR_IS_BLANK, SEV_ERROR, FLD_COMPONENT_NAME, 1, txnvars.buffer->GetField( FLD_COMPONENT_NAME ) );
		txnvars.Errors->Check();

		//@ Create transaction buffer
		buf = App->CreateBuffer( FMT_COMPONENT_DEFN );

		//@ Move screen info to transaction buffer
		buf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			txnvars.buffer->DebugDump();
			buf->DebugDump();

			//@ SUB ComponentVerify to verify that system value can be added
			ComponentVerify( buf, VERIFY_ADD, &txnvars, NULL );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB ComponentAdd to add the new system value
			ComponentAdd( buf, &txnvars );

			LOG_TRANSACTION();

			txnvars.buffer->MoveCorresponding( buf, txnvars.Errors );
			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &buf );
	END_TXN();
	//@ GRE End the transaction
};



//@ CHA Delete Component
//@ COD DeleteComponent
//@ NUM 37
//@ CRI FIELD_NAME
//@ GEN Delete Component will delete a Component Defn record
TRANSACTION_CODE( DeleteComponent ) 
{
	XCBuffer* buf = NULL;

	//@ GRS Start the transaction
	START_TXN( DeleteComponent );

		//@ Create transaction buffer
		buf = App->CreateBuffer( FMT_COMPONENT_DEFN, false );

		//@ Move screen info to transaction buffer
		buf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();
			//@ SUB ComponentVerify to verify that record can be deleted
			ComponentVerify( buf, VERIFY_DELETE, &txnvars, NULL );
			txnvars.Errors->Check();

			//@ SUB ComponentDelete to delete the record.
			ComponentDelete( buf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &buf );
	END_TXN();
	//@ GRE End the transaction
};

